package com.financialsharing.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.financialsharing.common.ServerResponse;
import com.financialsharing.controller.UserController;
import com.financialsharing.dao.CountDao;
import com.financialsharing.dao.OperateDao;
import com.financialsharing.dao.UserDao;
import com.financialsharing.pojo.Operate;
import com.financialsharing.pojo.User;
import com.financialsharing.service.JwtLoginService;
import com.financialsharing.util.JwtUtil;
import com.financialsharing.util.SHA256Util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * JWT业务验证类
 */
@Service
public class JwtLoginServiceImpl implements JwtLoginService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private CountDao countDao;

    @Autowired
    private OperateDao operateDao;

    Logger logger = LoggerFactory.getLogger(JwtLoginServiceImpl.class);

    @Override
    public ServerResponse<String> login(User user) {
        //验证成功返回token
        User u = userDao.checkLoginNameOrEmail(user.getLoginName(),user.getEmail());
        if(u == null){
            return ServerResponse.createByErrorMsg("登录名不存在");
        }
        //如果登陆成功
        if(u.getPassword().equals(SHA256Util.generateShaPwd(user.getPassword())) && u.getRoleId().equals(user.getRoleId())){
            //使用JWT加密生成token todo
            //置空密码
            u.setPassword(StringUtils.EMPTY);
            // todo important token有效期24个小时
            String token = JwtUtil.sign(u,60*60*24*1000L);
            /**
             * todo 统计网站访问次数：登陆成功后统计次数变量count加1
             */
            int num = countDao.selectCount();
            int count = ++num;
            countDao.updateOne(count);
            /**
             * 记录日志文件
             */
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            logger.warn("登录时间:{} 登录id:{} 登录用户:{} ",simpleDateFormat.format(date),u.getId(),u.getUsername());
            user.setPassword(StringUtils.EMPTY);
            Operate operate = new Operate("后台用户登录",u.getUsername(),u.getRoleId(),"登录成功",new Date(), JSONObject.toJSONString(user));
            operateDao.insertOperate(operate);
            return ServerResponse.createBySuccess(token);
        }else if(!(u.getRoleId().equals(user.getRoleId()))){
            user.setPassword(StringUtils.EMPTY);
            Operate operate = new Operate("后台用户登录",u.getUsername(),u.getRoleId(),"登录失败,角色错误",new Date(), JSONObject.toJSONString(user));
            operateDao.insertOperate(operate);
            return ServerResponse.createByErrorMsg("角色错误");
        }
        Operate operate = new Operate("后台用户登录",u.getUsername(),u.getRoleId(),"登录失败,登录密码错误",new Date(), JSONObject.toJSONString(user));
        operateDao.insertOperate(operate);
        return ServerResponse.createByErrorMsg("登录密码错误");
    }
}
